package com.sth.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sth.base.exceptionHandler.SthException;
import com.sth.edu.entity.Subject;
import com.sth.edu.entity.subject.OneSubject;
import com.sth.edu.entity.subject.TwoSubject;
import com.sth.edu.entity.vo.ExcelSubjectData;
import com.sth.edu.listener.SubjectExcelListener;
import com.sth.edu.mapper.SubjectMapper;
import com.sth.edu.service.SubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author sth
 * @since 2020-12-04
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    //添加课程分类
    //poi读取excel内容
    @Override
    public void importSubjectData(MultipartFile file, SubjectService subjectService) {
        try {
            //1 获取文件输入流
            InputStream inputStream = file.getInputStream();
            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new SthException(20002, "添加课程分类失败");
        }
    }

    //课程分类列表
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.查询出所有一级分类
        QueryWrapper<Subject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<Subject> oneSubjectList = baseMapper.selectList(wrapperOne);
        //2.查询出所有二级分类
        QueryWrapper<Subject> wrapperTwo = new QueryWrapper<>();
        wrapperOne.ne("parent_id", "0");
        List<Subject> twoSubjectList = baseMapper.selectList(wrapperTwo);
        // 创建list集合,用于封装最后的数据
        List<OneSubject> finSubjectList = new ArrayList<>();
        //3.封装一级分类
        for (int i = 0; i < oneSubjectList.size(); i++) {
            Subject subject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(subject, oneSubject);

            // 创建二级分类集合
            List<TwoSubject> twoFinSubjectList = new ArrayList<>();
            for (int m = 0; m < twoSubjectList.size(); m++) {
                Subject subject1 = twoSubjectList.get(m);
                if (subject1.getParentId().equals(oneSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(subject1, twoSubject);
                    twoFinSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinSubjectList);
            finSubjectList.add(oneSubject);
        }
        //4.封装二级分离
        return finSubjectList;
    }
}
